home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapter4 / adjwndrt.c < prev    next >
C/C++ Source or Header  |  1996-01-06  |  5KB  |  183 lines

  1.  
  2. #include <windows.h>  
  3. #include "AdjWndRt.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HWND      hWnd = NULL; 
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName   = "MyApp";
  20. LPCTSTR lpszClassName = "My Class";
  21.  
  22. BOOL InitAppInstance( HINSTANCE hInstance );
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  26.                       LPTSTR lpCmdLine, int nCmdShow)
  27. {
  28.     MSG  msg;
  29.     RECT rect;
  30.  
  31.     // Check to see if there is previous version of the application
  32.     // currently running. If not, then register the application class.
  33.     //................................................................
  34.     if ( !hPrevInstance && !InitAppInstance( hInstance ) )
  35.         return FALSE;
  36.  
  37.     hInst = hInstance;
  38.  
  39.     // Initialize a RECT with the size and position of 
  40.     // the client area we want.
  41.     //...................................
  42.     
  43.     rect.left   = 100; // 100 pixels from the left of the screen.
  44.     rect.top    = 150; // 150 pixels from the top of the screen.
  45.     rect.right  = 200; // The width will be 100 pixels.
  46.     rect.bottom = 200; // the height will be 50 pixels.
  47.  
  48.     AdjustWindowRect( &rect, WS_OVERLAPPEDWINDOW, TRUE );    
  49.     
  50.     hWnd = CreateWindow( lpszClassName,  // Class name registered.
  51.                    "My Application",     // Title bar text.
  52.                     WS_OVERLAPPEDWINDOW, // Window style.
  53.                     rect.left,           // Use the rect calculated
  54.                     rect.top,            //   with AdjustWindowRect()
  55.                     rect.right,          //   for the size and
  56.                     rect.bottom,         //   position.
  57.                     NULL,                // No parent.
  58.                     NULL,                // Use class menu.
  59.                     hInstance,           
  60.                     NULL);               
  61.  
  62.     if ( !hWnd )
  63.        return( FALSE );
  64.  
  65.     ShowWindow( hWnd, nCmdShow ); 
  66.     UpdateWindow( hWnd );         
  67.  
  68.     while ( GetMessage( &msg, NULL, 0, 0 ) )
  69.     {
  70.        TranslateMessage(&msg);
  71.        DispatchMessage(&msg); 
  72.     }
  73.     
  74.     return( TRUE );
  75. }
  76.  
  77.  
  78. BOOL InitAppInstance( HINSTANCE hInstance )
  79. {
  80.    WNDCLASS wc;
  81.  
  82.    // Register the main application window class.
  83.    //............................................
  84.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  85.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  86.    wc.cbClsExtra    = 0;                      
  87.    wc.cbWndExtra    = 0;                      
  88.    wc.hInstance     = hInstance;              
  89.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  90.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  91.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  92.    wc.lpszMenuName  = lpszAppName;              
  93.    wc.lpszClassName = lpszClassName;              
  94.  
  95.    if ( IS_WIN95 )
  96.    {
  97.       if ( !RegisterWin95( &wc ) )
  98.          return( FALSE );
  99.    }
  100.    else if ( !RegisterClass( &wc ) )
  101.       return( FALSE );
  102.  
  103.    return( TRUE );
  104. }
  105.  
  106.  
  107. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  108. {
  109.    WNDCLASSEX wcex;
  110.  
  111.    wcex.style         = lpwc->style;
  112.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  113.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  114.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  115.    wcex.hInstance     = lpwc->hInstance;
  116.    wcex.hIcon         = lpwc->hIcon;
  117.    wcex.hCursor       = lpwc->hCursor;
  118.    wcex.hbrBackground = lpwc->hbrBackground;
  119.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  120.    wcex.lpszClassName = lpwc->lpszClassName;
  121.  
  122.    // Added elements for Windows 95.
  123.    //...............................
  124.    wcex.cbSize = sizeof(WNDCLASSEX);
  125.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  126.                             IMAGE_ICON, 16, 16,
  127.                             LR_DEFAULTCOLOR );
  128.             
  129.    return RegisterClassEx( &wcex );
  130. }
  131.  
  132. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  133. {
  134.    switch( uMsg )
  135.    {
  136.       case WM_COMMAND :
  137.               switch( LOWORD( wParam ) )
  138.               {
  139.                  case IDM_ABOUT :
  140.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  141.                         break;
  142.  
  143.                  case IDM_EXIT :
  144.                         DestroyWindow( hWnd );
  145.                         break;
  146.               }
  147.               break;
  148.       
  149.       case WM_DESTROY :
  150.               PostQuitMessage(0);
  151.               break;
  152.  
  153.       default :
  154.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  155.    }
  156.  
  157.    return( 0L );
  158. }
  159.  
  160.  
  161. LRESULT CALLBACK About( HWND hDlg,           
  162.                         UINT message,        
  163.                         WPARAM wParam,       
  164.                         LPARAM lParam)
  165. {
  166.    switch (message) 
  167.    {
  168.        case WM_INITDIALOG: 
  169.                return (TRUE);
  170.  
  171.        case WM_COMMAND:                              
  172.                if (   LOWORD(wParam) == IDOK         
  173.                    || LOWORD(wParam) == IDCANCEL)    
  174.                {
  175.                        EndDialog(hDlg, TRUE);        
  176.                        return (TRUE);
  177.                }
  178.                break;
  179.    }
  180.  
  181.    return (FALSE); 
  182. }
  183.